<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>凡客-购物车</title>
		<link rel="stylesheet" type="text/css" href="<?php echo __PUBLIC__?>/Home/org/bootstrap-3.3.5-dist/css/bootstrap.min.css" />
		<script src="<?php echo __PUBLIC__?>/Home/js/jquery-1.7.2.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="<?php echo __PUBLIC__?>/Home/org/bootstrap-3.3.5-dist/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>

		<link rel="stylesheet" type="text/css" href="<?php echo __PUBLIC__?>/Home/css/shopcar.css" />
		
		<!-- 弹窗插件 -->
		<script src="<?php echo __PUBLIC__?>/Home/js/qikoo.js" type="text/javascript" charset="utf-8"></script>
		<link rel="stylesheet" type="text/css" href="<?php echo __PUBLIC__?>/Home/css/qikoo.css" />
	</head>

	<body>
		<div id="top">
			<a href="<?php echo U('Index/index')?>" class="logo"><img src="<?php echo __PUBLIC__?>/Home/images/vancl-logo.png" alt="" /></a>
			<div class="right">
				<a href="" class="help">帮助中心</a>
				<p>您好,
		            <?php if(isset($_SESSION['hname'])){?>
                
		            <span><a href="" style="color:#A10000;"><?php echo $_SESSION['hname']?></a> <a href="<?php echo U('Login/out')?>" style="color:#A10000;"> 退出登陆 </a> | <a href="<?php echo U('Login/login')?>" style="color:#A10000;">切换用户</a></span>
		            <?php }else{?>
		            欢迎光临凡客诚品！
		            <span><a href="<?php echo U('Login/login')?>">登录</a> | <a href="<?php echo U('Register/register')?>">注册</a></span>
		            
               <?php }?>
				</p>
			</div>
		</div>
		<!--购买流程-->
		<div id="process">
			<em>1.我的购物车</em>
			<span>2.填写核对信息单</span>
			<span>3.成功提交订单</span>
		</div>
		
		<?php if(isset($_SESSION['cart']['goods']) && !empty($_SESSION['cart']['goods'])){?>
                
		<!--购物车内容显示-->
		<div id="title">
			<span></span>
			<h2>我的购物车</h2>
		</div>
			<!--提示-->
		<div id="tips">
			<span>温馨提示：</span>1.选购清单中的商品无法保留库存，请您及时结算。2.商品的价格和库存将以订单提交时为准。3.促销活动满减优惠将均摊至商品小计中。 
		</div>
			<!--具体内容-->

			<!-- 全选js -->
			<script type="text/javascript">
				$(function(){
					var x = 0;
					$('#all_check,#all_ckeck_xia,#all_check_lab').click(function() {
						if(x==0){
							$('input:checkbox').attr('checked', 'checked');
							x=1;
						}else{
							$('input:checkbox').removeAttr('checked');
							x=0;
						}
					});
				})
			</script>

			<!-- 点击增加js -->
			<script type="text/javascript">
				$(function(){
					$('.plus').click(function() {
						var x = parseInt($(this).parent('.shop_num').find('input').val());
						x = x+1;
						
						$(this).parent('.shop_num').find('input').val(x);

						// 判断是否选中
						var demo = $(this).parents('tr').find('.tr_check:checked').length;
						// alert(demo);
						if(demo==1){
							var num = parseInt($('.shop_count_title em').text());
							var total = parseInt($('.shop_total em').text());
							
							num = num + 1;
							total = total + parseInt($(this).parents('tr').find('.unit_price span').text());
							
							$('.shop_count_title em').html(num);
							$('.shop_total em').html(total);
						}
					});
					$('.subtract').click(function() {
						var x = parseInt($(this).parent('.shop_num').find('input').val());
						if(x<=1){
							$(this).parent('.shop_num').find('input').val(1);
							return;
						}
						x = x-1;
						$(this).parent('.shop_num').find('input').val(x);

						// 判断是否选中
						var demo = $(this).parents('tr').find('.tr_check:checked').length;
						// alert(demo);
						if(demo==1){
							var num = parseInt($('.shop_count_title em').text());
							var total = parseInt($('.shop_total em').text());
							
							num = num - 1;
							total = total - parseInt($(this).parents('tr').find('.unit_price span').text());
							
							$('.shop_count_title em').html(num);
							$('.shop_total em').html(total);
						}
					});
				})
			</script>
	
	<form action="<?php echo U('Order/index')?>" method="post">
		<div id="content_car">
			
			<table cellpadding="0" id="shop_table">
				<tr>
					<th class="white"></th>
					<th class="bar"><input type="checkbox" id="all_check" /> <span>全选</span></th>
					<th class="img"></th>
					<th class="name">商品名称</th>
					<th class="size">颜色</th>
					<th class="size">尺码</th>
					<th class="price">单价</th>
					<th class="num">数量</th>
					<th class="privilege">优惠</th>
					<th class="subtotal">小计</th>
					<th class="operation">操作</th>
					<th class="white"></th>
				</tr>
				
				<script type="text/javascript">
					$(function(){
						// 进入页面后默认选中
						$('.tr_check').attr('checked', 'checked');
						// 单击后设置
						$('.subtract,.plus').click(function() {
							// 组数组
							var goods_up = {
								'sid':$(this).parents('tr').find('.tr_check').val(),
								'num':$(this).parents('.shop_num').find('input').val()
							};
							// console.log(goods_up);
							var demo = $(this).parents('tr');
							// 异步改变session
							$.ajax({
								url: '<?php echo U('ajaxUp')?>',
								type: 'POST',
								dataType: 'json',
								data: {goods_up: goods_up},
								success:function(arr){
									// console.log(arr);
									demo.find('.unit_total span').html(arr.unit_total);
									// $('.shop_count_title').find('em').html(arr.total_rows);
									// $('.shop_total').find('em').html(arr.total);
								}
							})

						});


						var oldnum = 0;
						var oldprice = 0;
						$('.shop_num input').focus(function() {
							// 获取原数值
							oldnum = parseInt($(this).parents('.shop_num').find('input').val());
							oldprice = parseInt($(this).parents('tr').find('.unit_total span').text());
						});
						
						// 失去焦点事件
						$('.shop_num input').blur(function() {
							// 判断数值是否超出范围
							if($(this).parents('.shop_num').find('input').val()<=0){
								$(this).parents('.shop_num').find('input').val(1);
							}

							
							// 组数组
							var goods_up = {
								'sid':$(this).parents('tr').find('.tr_check').val(),
								'num':$(this).parents('.shop_num').find('input').val()
							};
							// console.log(goods_up);
							var demo = $(this).parents('tr');
							// 异步改变session
							$.ajax({
								url: '<?php echo U('ajaxUp')?>',
								type: 'POST',
								dataType: 'json',
								data: {goods_up: goods_up},
								success:function(arr){
									// console.log(arr);
									demo.find('.unit_total span').html(arr.unit_total);
									
									// 判断是否选中
									var x = demo.find('.tr_check:checked').length;
									// alert(demo);
									if(x==1){
										var newnum = parseInt($('.shop_count_title').find('em').text()) - oldnum + parseInt(goods_up['num']);
										var newprice = parseInt($('.shop_total').find('em').text()) - oldprice + parseInt(arr.unit_total);
										// alert(newnum);
										$('.shop_count_title').find('em').html(newnum);
										$('.shop_total').find('em').html(newprice);
									}
								}
							})

						});
					})
				</script>

				<?php foreach ($_SESSION['cart']['goods'] as $k=>$v){?>
				<tr class="l_goods">
					<td class="white"></td>
					<td><input type="checkbox" class="tr_check" name="goods[]" value="<?php echo $k?>"/></td>
					<td class="shop_img"><img src="<?php echo $v['pic']?>" style="width:68px" /></td>
					<td class="shop_name"><a href="<?php echo U('Desc/index',array('gid'=>$v['id']))?>" target="_blank"><?php echo $v['name']?></a> </td>
						<?php foreach ($v['options'] as $kk=>$vv){?>
						<td><?php echo $vv?></td>
						<?php }?>
					<td class="unit_price">￥<span><?php echo $v['price']?></span></td>
					<td class="shop_num">
						<a href="javascript:;" class="subtract"></a>
						<input type="text" id="" value="<?php echo $v['num']?>" />
						<a href="javascript:;" class="plus"></a>
					</td>
					<td>-</td>
					<td class="unit_total">￥<span><?php echo $v['total']?></span></td>
					<td class="del"><a href="javascript:;">删除</a></td>
					<td class="white"></td>
				</tr>
				<?php }?>

				

				<tr class="index">
					<td class="white"></td>
					<td colspan="10"></td>
					<td class="white"></td>
				</tr>
			</table>
			<!-- 删除js -->
			<script type="text/javascript">
				$(function(){
					$('.del a').click(function() {
						$('.question').remove();
						// 显示弹框
						if($(this).next('div.question').length <= 0)
							$(this).after('<div class="question">确定删除该商品吗？<br/> <span class="yes">确定</span><span class="cancel">取消</span></div>');
						$('.question').animate({opacity: 1}, 300);

						// 判断确定与取消
						$('.yes').live('click', function(){
							// 获得当前货品  id
							var data = {
								'sid':$(this).parents('tr').find('.tr_check').val(),
								'num':0
							};
							var _this=$(this);
							var num = parseInt($('.shop_count_title em').text());
							var total = parseInt($('.shop_total em').text());
							// 发送ajax
							$.ajax({
								url: '<?php echo U('ajaxUp')?>',
								type: 'POST',
								dataType: 'json',
								data: {data,data},
								success:function(arr){
									if(arr.length==0){
										// 判断是否选中
										var demo = _this.parents('tr').find('.tr_check:checked').length;
										// alert(demo);
										if(demo==1){
											num = num - parseInt(_this.parents('tr').find('.shop_num input').val());
											total = total - parseInt(_this.parents('tr').find('.unit_total span').text());
											$('.shop_count_title em').html(num);
											$('.shop_total em').html(total);
										}
										_this.parents('tr').remove();
									}
								}
							})
							
						});

						// 取消隐藏弹窗
						$('.cancel').live('click', function(){
							$(this).parents('div.question').fadeOut(300, function() {
								$(this).parents('div.question').remove();
							});
						});
					});
					
				})
			</script>

			<!-- // 删除多个商品 -->
			<script type="text/javascript">
				$(function(){
					$('#del_tr').click(function() {
						var x = $('.tr_check:checked').length;
						if(x==0){
							qikoo.dialog.alert("请选择要删除的商品！");
							return;
						}
						qikoo.dialog.confirm('确定要删除此商品吗？',function(){
								// 声明空数组
								var arr = new Array();
								// 循环添加
								$.each($('.tr_check:checked'), function(k, v) {
									 arr[k] = {'sid':$(this).parents('tr').find('.tr_check').val(),'num':0}
								});
								// console.log(arr);
								$.ajax({
									url: '<?php echo U('ajaxDel')?>',
									type: 'POST',
									dataType: 'json',
									data: {arr: arr},
									success:function(){
										$.each($('.tr_check:checked'), function(k, v) {
											$(this).parents('tr').remove();
											$('.shop_count_title em').html(0);
											$('.shop_total em').html(0);
										});
									}
								})
					        });
					});
					
				})
			</script>

			<!-- 总计js -->
			<script type="text/javascript">
				$(function(){
					// 默认设置
					var num = 0;
					var total = 0;
					$.each($('.tr_check:checked'), function(k, v) {
						num = num + parseInt($(this).parents('tr').find('.shop_num input').val());
						total = total + parseInt($(this).parents('tr').find('.unit_total span').text());
					});
					$('.shop_count_title em').html(num);
					$('.shop_total em').html(total);

					// 选中设置
					$('.tr_check,#all_ckeck_xia,#all_check').click(function() {
						var num = 0;
						var total = 0;
						$.each($('.tr_check:checked'), function(k, v) {
							num = num + parseInt($(this).parents('tr').find('.shop_num input').val());
							total = total + parseInt($(this).parents('tr').find('.unit_total span').text());
						});
						$('.shop_count_title em').html(num);
						$('.shop_total em').html(total);
					});
				})
			</script>

			<div class="shop_count">
				<div class="shop_count_title">
					<input type="checkbox" id="all_ckeck_xia"/>
					<lable id="all_check_lab">全选</lable>
					<a href="javascript:;" id="del_tr">删除</a>
					<span>数量总计：<em></em> 件</span>
					<span><strong>您目前可享受全场购物免运费</strong> </span>
				</div>
				<div class="shop_total">
					产品金额总计(不含运费)：<span>￥<em></em></span>
				</div>
					<button class="settlement">去结算>></button>
				<a href="<?php echo U('Index/index')?>" class="go_shop"><<继续购物</a>
			</div>
			
		</div>
	</form>
		<?php }else{?>
		<div id="hide">
			您的购物车中没有商品，请您先 <a href="<?php echo U('Index/index')?>">选购商品»</a>
		</div>
		
               <?php }?>
		<!--网页结束-->
		<div id="bottom">
			<p>Copyright 2007 - 2016 vancl.com All Rights Reserved 出版物经营许可证新出发京批字第直xxxxx号</p>
			<div>
				<a href="" class="index_1"><img src="<?php echo __PUBLIC__?>/Home/images/cert-redlogo.gif.png"/></a>
				<a href="" class="index_2"></a>
				<a href="" class="index_3"></a>

			</div>
		</div>
	</body>
</html>